﻿#region 命名空间

using System;
using System.Runtime.Serialization;
using System.ServiceModel;

#endregion

namespace Sharp.DataTransferObject
{
    /// <summary>
    ///     服务端异常载体
    /// </summary>
    [DataContract]
    public class FaultData
    {
        #region 属性

        /// <summary>
        ///     消息
        /// </summary>
        [DataMember(Order = 0)]
        public string Message { get; set; }

        /// <summary>
        ///     完整消息
        /// </summary>
        [DataMember(Order = 1)]
        public string FullMessage { get; set; }

        /// <summary>
        ///     堆栈
        /// </summary>
        [DataMember(Order = 2)]
        public string StackTrace { get; set; }

        #endregion

        /// <summary>
        ///     创建自定义异常
        /// </summary>
        /// <param name="ex"></param>
        /// <returns></returns>
        public static FaultData CreateFromException(Exception ex)
        {
            return new FaultData
            {
                Message = ex.Message,
                FullMessage = ex.ToString(),
                StackTrace = ex.StackTrace
            };
        }

        /// <summary>
        ///     创建SOAP异常
        /// </summary>
        /// <param name="ex"></param>
        /// <returns></returns>
        public static FaultReason CreateFaultReason(Exception ex)
        {
            return new FaultReason(ex.Message);
        }
    }
}